<?php

namespace app\system\controller;

use app\common\controller\Excel;
use think\Db;
use think\Exception;
use think\Request;

class Level extends Common
{
    protected $table = 'level_agent', $name = '代理等级', $html = 'index_agent';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $type = input('type');
        switch ($type) {
            case 1:
                $this->table = 'level_agent';
                $this->name = '会员等级';
                break;
            case 2:
                $this->table = 'level_think';
                $this->name = '会员等级';
                $this->html = 'index_think';
                break;
            case 3:
                $this->table = 'level_service';
                $this->name = '分公司等级';
                $this->html = 'index_service';
                break;
            case 4:
                $this->table = 'level_partner';
                $this->name = '合伙人等级';
                $this->html = 'index_partner';
                break;
            case 5:
                $this->table = 'level_group';
                $this->name = '团长等级';
                $this->html = 'index_group';
                break;
            case 6:
                $this->table = 'level_gold';
                $this->name = '金豆等级';
                $this->html = 'index_gold';
                break;
            case 7:
                $this->table = 'member_level';
                $this->name = '会员等级等级';
                $this->html = 'member_level';
                break;
        }
    }

    private function select()
    {
        return db($this->table . ' t');
    }

    function index()
    {
        $list = $this->select()->where('is_del=0')->paginate(20, false, ['query' => request()->get()]);

        $this->assign('list', $list);
        return $this->fetch($this->html);
    }

    function info($id)
    {
        $info = db($this->table . ' t')->where(['t.id' => $id])->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    function add($id = '')
    {
        if ($id) {
            //数据
            $info = db($this->table)->find($id);
        } else {
            //默认数据
            $info = ['radio' => '0'];
        }
        $level_name=['会员','合伙人','代理分公司','分公司'];
        $this->assign('info', $info);
        $this->assign('level_name', $level_name);

        return $this->fetch();
    }

    function save($id = '', $type = '')
    {
        if (request()->isPost()) {
            Db::startTrans();
            try {
                //数据
                $data = input('post.');
                $data['update_time'] = time();
                switch ($type) {
                    case 1:
                        $data['red_min'] = $data['red_min'] / 100;
                        $data['red_max'] = $data['red_max'] / 100;
                        break;
                    case 2:
                        $data['rec1'] = $data['rec1'] / 100;
                        $data['rec2'] = $data['rec2'] / 100;
                        $data['renew_rec1'] = $data['renew_rec1'] / 100;
                        $data['renew_rec2'] = $data['renew_rec2'] / 100;
                        // $tp = $data['tp'] / 100;
                        // $data['amount'] = $data['amount'] * 10000;
                        // $data['team_amount'] = $data['team_amount'] * 10000;
                        // $data['bonus'] = $data['bonus'] / 100;
                        // unset($data['tp']);
                        /* db($this->table)->where('id >= ' . $id . '')->update(['tp_' . $id => $tp]);
                        //设置封顶了，代理等级的封顶将失效，按照此封顶走
                        if($data['capping'] && $data['capping'] > 0){
                            //1.查询出所有此等级的会员
                            $list = db('member')->where('think_id',$id)->field('id,agent_money,agent_capping,agent_capping_amount')->select();
                            foreach ($list as $k=>$v){
                                //2.计算封顶金额
                                $money = $v['agent_money'] * $data['capping'];
                                //3.更改封顶倍数与封顶金额
                                db('member')->where('id',$v['id'])->update([
                                    'agent_capping' => $data['capping'],
                                    'agent_capping_amount' => $money
                                ]);
                            }
                        } */
                        break;
                    case 3:
                        $data['tp'] = $data['tp'] / 100;
                        break;
                    case 4:
                        $data['amount'] = $data['amount'] * 10000;
                        $data['tp'] = $data['tp'] / 100;
                        break;
                    case 5:
                    case 6:
                        $data['tp'] = $data['tp'] / 100;
                        break;

                }

                if ($id == '') {
                    //新增
                    $data['create_time'] = time();
                    $res = db($this->table)->insertGetId($data);
                    $id = $res;
                    $action = '添加';
                } else {
                    //修改
                    $res = db($this->table)->where(['id' => $id])->update($data);
                    $action = '修改';
                }
                if ($res) {

                    //整体成功
                    Db::commit();
                    action('common/Logs/addLog', ['管理员[' . $this->admin['username'] . ']' . $action . '了' . $this->name . '，' . $this->name . 'ID[' . $id . ']']);
                    return return_message('1', '保存成功');
                } else {
                    Db::rollback();
                    return return_message('0', '保存失败', '3');
                }
            } catch (Exception $e) {
                Db::rollback();
                return return_message('0', '保存失败'.$e->getMessage(), '2');
            }
        } else {
            return return_message('0', '保存失败', '1');
        }
    }

    /**
     * @param $id
     * @param array $images
     * @return array
     * 多图片保存
     */
    function saveImages($id, array $images)
    {
        $res = $this->createChildTableDatas($this->table_images, ['upid' => $id], $images, function ($v) use ($id) {
            return ['upid' => $id, 'img' => $v];
        });
        return $res;
    }

    function state($id, $key, $val)
    {
        return $this->editState($this->table, $id, $key, $val);
    }

    function value($id, $key, $val)
    {
        return $this->editValue($this->table, $id, $key, $val);
    }

    function export()
    {
        $excelc = new Excel();

        $map = [
            ['title' => 'title', 'key' => 'title', 'width' => '20'],
            ['title' => 'create_time', 'key' => 'create_time'],
            ['title' => '状态', 'key' => 'radio'],
        ];

        $data = $this->select()->select();
        foreach ($data as $k => &$v) {
            $v['create_time'] = date('Y-m-d', $v['create_time']);
            $v['radio'] = $v['radio'] == '1' ? '显示' : '隐藏';
        }

        $excelc->export($this->name . date('YmdHis'), $map, $data);
    }
}